# -*- coding: utf-8 -*-

'''
@author: Pedro Lindeza 9636, Ricardo Braga 10328
@date: Dezembro de 2014
Cria o gráfico tendo a Base de Dados como fonte
'''

import sqlite3

import numpy as np
import matplotlib.pyplot as plt

'''
Abre a Base de Dados
'''
conn = sqlite3.connect('IPC_Portugal_1977_2013.db')
cmd = conn.cursor()
sql = 'SELECT * FROM data'

graphArray = []

'''
Separa as informações retiradas do ficheiro
'''
for row in cmd.execute(sql):
	startingInfo = str(row).replace('u''', '')
	splitInfo = startingInfo.split(',')
	graphArrayAppend = splitInfo[0] + ',' + splitInfo[1] + ',' + splitInfo[2] + ',' + splitInfo[3] + ',' + splitInfo[4] + ',' + splitInfo[5] + ',' + splitInfo[6] + ',' + splitInfo[7] + ',' + splitInfo[8]
	graphArray.append(graphArrayAppend)
	print graphArrayAppend

'''
Desenha o gráfico
'''
fig = plt.figure()

xs = [1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013]
ys = [100.0, 122.1, 150.6, 175.8, 211.9, 260.1, 325.2, 419.8, 503.3, 561.9, 614.6, 676.3, 761.7, 865.8, 971.8, 1063.5, 1135.1, 1196.8, 1246.8, 1284.5, 1314.4, 1351.2, 1382.9, 1422.4, 1485.0, 1538.7, 1589.3, 1626.9, 1664.0, 1715.3, 1757.4, 1803.0, 1787.8, 0, 0, 0, 0]

ax = fig.add_subplot(111)
ax.bar(xs, ys)
ax.set_xlabel('Anos')
ax.set_ylabel('IPC Global')

plt.show()
